"""
@Author: marco
@Date: 2021-6-17 09:26:00
@description: app初始化
@LastEditors  : marco
@LastEditTime : 2021-6-17 09:26:00
"""
from apscheduler.schedulers.blocking import BlockingScheduler
from flask import Flask

from app.utils.exception import exception_api
from app.utils.jr import JR
# 权限模块 https://github.com/raddevon/flask-permissions
# from flask_permissions.core import Permissions
from app.enums.code import Code
from app.server.api import api
from app.env import SQLALCHEMY_DATABASE_URI, SQLALCHEMY_TRACK_MODIFICATIONS
import environment as e

# 读取启动环境
environment = e.read()

# 普通json带error_code风格使用此app示例
app = Flask(__name__)


# # 配置 sqlalchemy  数据库驱动://数据库用户名:密码@主机地址:端口/数据库?编码
# app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
# app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = SQLALCHEMY_TRACK_MODIFICATIONS
# # 上传文件配置
# app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER  # 上传目录
# app.config['MAX_CONTENT_LENGTH'] = MAX_CONTENT_LENGTH  # 上传大小
# # 创建数据库及连接
# engine = create_engine(SQLALCHEMY_DATABASE_URI)
# # 创建DBSession类型:
# DBSession = sessionmaker(bind=engine)
# dBSession = DBSession()


# @app.teardown_appcontext
# def shutdown_session(exception=None):
#     dBSession.close()


# 挂载500异常处理,并记录日志
# environment == 'restful':
@api.errorhandler(Exception)
def error_handler(e):
    return exception_api(Code.ERROR, e)


# 引入使用的控制器
# if environment == 'run' or environment == 'restful':
#     # 蓝图，新增的后台部分代码
#     from app.Controllers.AdminController import admin
#
#     app.register_blueprint(admin, url_prefix='/admin')

if environment == 'job':
    # 任务调剂
    sched = BlockingScheduler()
    # 引入任务
    # from app.job import sync_cron_job
    from app.job import predict_cron_job
